package com.season.seasonStudy.javase.j_021_io;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class TestIOAction {
	
	public static void main(String[] args) {
		//File类不读写文件，只是用于查看文件的属性
		/*File file=new File("f:/aaa");
		if (file.exists()) {
			System.out.println("文件存在");
		}
		if (file.isFile()) {
			System.out.println("是一个文件");
		}
		if (file.isDirectory()) {
			System.out.println("是一个文件夹");
		}
		System.out.println("相对路径："+file.getPath());
		System.out.println("绝对路径："+file.getAbsolutePath());
		System.out.println("文件的名字是："+file.getName());
		System.out.println("文件长度"+file.length());*/
		/*if (file.delete()) {
			System.out.println("删除成功");
		}
		try {
			if (file.createNewFile()) {
				System.out.println("创建成功");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
		
		/*
		File[] files=file.listFiles();
		for (int i = 0; i < files.length; i++) {
			System.out.println(files[i].getName());
		}
		*/
		
		
		//字节流读写文件
		/*File file=new File("f:/aaa/a.txt");
		FileInputStream fis=null;
		FileOutputStream fos=null;
		StringBuffer sb=new StringBuffer();
		try {
			 fis=new FileInputStream(file);
			 fos=new FileOutputStream("f:/aaa/b.txt",true);
			System.out.println("文件长度："+fis.available());
			
			byte[] b=new byte[fis.available()];
			System.out.println("int:"+fis.read(b));
			for (int i = 0; i < b.length; i++) {
				System.out.print((char)b[i]);
			}
			char[] b1=new char[]{'a','b'};
			sb.append(b1);
			String s=sb.toString();
			System.out.println("stringbuffer:"+s);
			
			fos.write(b);
			fos.flush();
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				fis.close();
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}*/
		
		
		//字符流文件读写
		/*BufferedReader br=null;
		BufferedWriter bw=null;
		StringBuffer sb=new StringBuffer();
		String s=null;
			try {
				Reader r=new FileReader("f:/aaa/a.txt");
				br=new BufferedReader(r);
				while((s=br.readLine())!=null){
					sb.append(s);
				}
				s=sb.toString();
				System.out.println("之前的："+s);
				
				br.close();
				r.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			try {
				Writer w=new FileWriter("f:/aaa/a.txt");
				bw=new BufferedWriter(w);
				s=s.replace("{name}", "欧欧");
				s=s.replace("{type}", "狗狗");
				s=s.replace("{master}", "李伟");
				System.out.println("之后的："+s);
				bw.write(s);
				bw.close();
				w.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		*/
		
		//二进制文件读写
		FileInputStream fis=null;
		FileOutputStream fos=null;
		DataInputStream dis=null;
		DataOutputStream dos=null;
		try {
			fis=new FileInputStream("e:/7103.JPG");
			fos=new FileOutputStream("f:/7103.JPG");
			dis=new DataInputStream(fis);
			dos=new DataOutputStream(fos);
			int a=0;
			try {
				a=dis.read();
				while(a!=-1){
					dos.write(a);
					a=dis.read();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}finally{
			try {
				dis.close();
				dos.close();
				fis.close();
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		
		
		
		
		
		
		/*List<Pet> list=new ArrayList<Pet>();
		Dog d1=new Dog("aaa", "拉布拉多");
		Dog d2=new Dog("bbb", "金毛");
		Dog d3=new Dog("ccc", "雪纳瑞");
		Dog d4=new Dog("ddd", "泰迪");
		Cat c=new Cat();
		System.out.println(d4);
		System.out.println(c);
		list.add(d1);
		
		Pet d=list.get(0);
		list.add(d1);
		list.add(d3);
		list.add(d4);
		list.add(2,d2);
		list.add(d4);
		System.out.println(list);
		for (int i = 0; i <list.size(); i++) {
			System.out.println(list.get(i));
		}
		Dog d=(Dog)list.get(0);
		
		Dog removeDog=(Dog)list.remove(0);
		System.out.println("删除的狗是"+removeDog);
		boolean answer=list.remove(d4);
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}
		
		Map<Integer, Dog> map=new HashMap<Integer, Dog>();
		map.put(1, d1);
		map.put(2, d2);
		map.put(3, d3);
		map.put(4, d4);
		//map.remove(4);
		if (map.containsKey(4)) {
			for (int i = 0; i < map.size(); i++) {
			System.out.println(map.get(i+1));
			}
		}else{
			System.out.println("存在");
		}
		System.out.println(map.values());
		System.out.println(map.keySet());
		Set<Integer> set=map.keySet();
		Iterator<Integer> it=set.iterator();
		while(it.hasNext()){
			
			System.out.println(it.next());
		}
		
		Demo<Dog> demo=new Demo<Dog>();
		*/
		
		
		
	}
}
